package com.hlb.manager.error;

import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.error.ErrorAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author hanlibin
 * @Description:
 * @date 2018/7/2112:38
 */
public class MyErrorController extends BasicErrorController {

    public MyErrorController(ErrorAttributes errorAttributes, ErrorProperties properties, List<ErrorViewResolver> errorViewResolvers) {
        super(errorAttributes, properties, errorViewResolvers);
    }

    @Override
    protected Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {

        Map<String, Object> map=super.getErrorAttributes(request, includeStackTrace);
        map.remove("timestamp");
        map.remove("status");
        map.remove("error");
        map.remove("exception");
        map.remove("path");
        map.put("code",ErrorEnum.getByCode((String) map.get("message")).getCode());
        map.put("message",ErrorEnum.getByCode((String) map.get("message")).getMessage());
        map.put("canRetry",ErrorEnum.getByCode((String) map.get("message")).isCanRetry());
        return map;

    }
}
